package invoice;

import invoice.footer.*;
import invoice.header.*;
import java.util.LinkedList;

class HeaderAndFooterList {
    private LinkedList<Header> headerList;
    private LinkedList<Footer> footerList;

    public HeaderAndFooterList(){
        headerList= new LinkedList<>();
        footerList= new LinkedList<>();
    }

    public void add(Invoice header){
        headerList.add((Header)header);
        addFooter(header);
    }

    public void add(Invoice[] header){
        for(Invoice hd:header){
            headerList.add((Header)hd);
            addFooter(hd);
        }
    }

    public void decorate() {
        for (int i = headerList.size() - 1; i > 0; i--) {
            if (headerList.get(i) != null)
                headerList.get(i).decorate(headerList.get(i - 1));
            else {
                System.out.println("有发票头为空！");
                System.exit(0);
            }
        }

        for (int i = footerList.size() - 1; i > 0; i--) {
            if(footerList.get(i)!=null)
                footerList.get(i).decorate(footerList.get(i - 1));
            else{
                System.out.println("有发票尾为空！");
                System.exit(0);
            }
        }
    }

    public void printHeader(){
        headerList.getLast().print();
    }

    public void printFooter(){
        footerList.getLast().print();
    }

    private void addFooter(Invoice header){
        if(header instanceof ChineseHeader)
            footerList.add(new ChineseFooter());
        else if(header instanceof EnglishHeader)
            footerList.add(new EnglishFooter());
    }
}
